{template 'manage/header'}
<div class="page" id="page-manage-order">
	<header class="bar bar-nav common-bar-nav">
		<a class="icon pull-left fa fa-arrow-left back"></a>
		<h1 class="title">订单管理</h1>
	</header>
	{template 'manage/nav'}
	<div class="content infinite-scroll" data-distance="50" data-min="{$min}">
		<div class="buttons-tab">
			<a href="{php echo $this->createMobileUrl('mgorder', array('status' => 1));}" class="button {if $status == 1}active{/if}">待确认</a>
			<a href="{php echo $this->createMobileUrl('mgorder', array('status' => 2));}" class="button {if $status == 2}active{/if}">处理中</a>
			<a href="{php echo $this->createMobileUrl('mgorder', array('status' => 3));}" class="button {if $status == 3}active{/if}">待配送</a>
			<a href="{php echo $this->createMobileUrl('mgorder', array('status' => 4));}" class="button {if $status == 4}active{/if}">配送中</a>
			<a href="javascript:;" class="button {if in_array($status, array(5, 6, 0))}active{/if} open-popover" data-popover=".popover-order-status">{if in_array($status, array(5, 6, 0))}{$order_status[$status]['text']}{else}更多{/if} <i class="fa fa-arrow-down"></i></a>
		</div>
		{if empty($orders)}
			<div class="no-data">
				<div class="bg"></div>
				<p>没有任何订单哦～</p>
			</div>
		{else}
		<div class="order-list">
			<ul>
				{loop $orders $order}
					<li>
						<a class="order-ls-info" href="{php echo $this->createMobileUrl('mgorder', array('op' => 'detail', 'id' => $order['id']));}">
							<div class="order-ls-tl">下单人:{$order['username']}<span class="{$order_status[$order['status']]['color']}">{$order_status[$order['status']]['text']}</span></div>
							<div class="order-ls-date">{php echo date('Y-m-d H:i:s', $order['addtime']);}<span>编号: {$order['id']}</span></div>
							<div class="order-ls-dl">
								{loop $order['goods'] $good}
								<div class="row">
									<div class="col-60">{$good['goods_title']}</div>
									<div class="col-20 align-right">X {$good['goods_num']}</div>
									<div class="col-20 align-right">¥{$good['goods_price']}</div>
								</div>
								{/loop}
								<div class="row">
									<div class="col-60">配送费¥{$order['delivery_fee']} + 包装费¥{$order['pack_fee']}</div>
									<div class="col-40 align-right">¥{$order['total_fee']}元</div>
								</div>
							</div>
							<div class="order-ls-sum">
								共{$order['num']}件，合计：¥{$order['final_fee']}
								<span class="color-danger">(已优惠¥{$order['discount_fee']})</span>
								<span class="pull-right color-primary order-ls-dist hide" data-lat="{$order['location_x']}" data-lng="{$order['location_y']}"></span>
							</div>
						</a>
						<div class="order-ls-btn">
							{if $order['status'] < 5}
								{if $order['status'] == 1}
									<a href="javascript:;" class="order-status" data-id="{$order['id']}" data-status="2" data-type="handel"><i class="fa fa-selected"></i> 确认接单</a>
									<a href="javascript:;" class="order-cancel" data-id="{$order['id']}" data-status="6" data-pay="{$order['is_pay']}" data-pay-type="{$order['pay_type']}"><i class="fa fa-selected"></i> 取消订单</a>
								{elseif $order['status'] == 2 || $order['status'] == 3}
									<a href="javascript:;" class="order-status" data-id="{$order['id']}" data-status="3" data-type="delivery_wait"><i class="fa fa-selected"></i> 通知配送员配送</a>
									{if $account['delivery_type'] == 1}
										<a href="javascript:;" class="order-delivery" data-id="{$order['id']}"><i class="fa fa-selected"></i> 指定配送员配送</a>
									{/if}
									<a href="javascript:;" class="order-status" data-id="{$order['id']}" data-status="4" data-type="delivery_ing"><i class="fa fa-selected"></i> 设为配送中</a>
								{elseif $order['status'] == 4}
									<a href="javascript:;" class="order-status" data-id="{$order['id']}" data-status="5" data-type="end"><i class="fa fa-selected"></i> 订单完成</a>
								{/if}
							{/if}
						</div>
					</li>
				{/loop}
			</ul>
			<div class="infinite-scroll-preloader hide">
				<div class="preloader"></div>
			</div>
		</div>
		{/if}
	</div>
</div>
<div class="popover popover-manage popover-order-status">
	<div class="popover-angle"></div>
	<div class="popover-inner">
		<div class="list-block">
			<ul>
				<li><a href="{php echo $this->createMobileUrl('mgorder', array('status' => 5));}" class="list-button item-link">已完成</a></li>
				<li><a href="{php echo $this->createMobileUrl('mgorder', array('status' => 6));}" class="list-button item-link">已取消</a></li>
				<li><a href="{php echo $this->createMobileUrl('mgorder', array('status' => 0));}" class="list-button item-link">所有</a></li>
				<li><a href="javascript:;" class="list-button item-link close-popover">关闭</a></li>
			</ul>
		</div>
	</div>
</div>
<!-- 选择配送员 -->
<div class="popup popup-delivery" id="popup-delivery">
	<div class="page">
		<header class="bar bar-nav common-bar-nav">
			<h1 class="title">分配配送员</h1>
			<a class="pull-right close-popup" href="javascript:;">关闭</a>
		</header>
		<div class="content">
			{if !empty($deliveryers)}
			<div class="list-block">
				<ul>
					{loop $deliveryers $deliveryer}
					<li>
						<label class="label-checkbox item-content">
							<input type="radio" name="deliveryer_id" value="{$deliveryer['deliveryer']['id']}" checked>
							<div class="item-media"><i class="icon icon-form-checkbox"></i></div>
							<div class="item-inner">
								<div class="item-title">{$deliveryer['deliveryer']['title']}</div>
								<div class="item-after">{$deliveryer['deliveryer']['mobile']}</div>
							</div>
						</label>
					</li>
					{/loop}
				</ul>
			</div>
			<div class="content-block">
				<a href="javascript:;" class="button button-big button-fill button-danger">确定</a>
			</div>
			{else}
				<h3 class="align-center">您还没有添加配送员</h3>
			{/if}
		</div>
	</div>
</div>
<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=4c1bb2055e24296bbaef36574877b4e2"></script>
<script>
$(function(){
	$(document).on("pageInit", "#page-manage-order", function(e, id, page) {
		var loading = false;
		$(page).on('infinite', '.infinite-scroll',function() {
			var $this = $(this);
			var id = $this.data('min');
			if(!id) return;
			if (loading) return;

			loading = true;
			$this.find('.infinite-scroll-preloader').removeClass('hide');
			$.post("{php echo $this->createMobileUrl('mgorder', array('op' => 'more', 'status' => $status))}", {id: id, time: timeStamp}, function(data){
				var result = $.parseJSON(data);
				$this.attr('data-min', result.message.min);

				if(!result.message.min) {
					$.detachInfiniteScroll($('.infinite-scroll'));
					$('.infinite-scroll-preloader').remove();
					return;
				}

				$this.find('.infinite-scroll-preloader').removeClass('hide');
				var gettpl = $('#tpl-order').html();
				loading = false;
				laytpl(gettpl).render(result.message.message, function(html){
					setTimeout(function() {
						$this.find('.order-list ul').append(html);
						order_list_dist();
					}, 1000);
				});
			});
		});
		function order_list_dist() {
			{if $store['location_x'] && $store['location_y']}
				var map = new BMap.Map("allmap");
				var pointA = new BMap.Point("{$store['location_y']}", "{$store['location_x']}");
				$.each($('.order-ls-dist'), function(){
					var lat = $(this).data('lat');
					var lng = $(this).data('lng');
					if(lat && lng) {
						var pointB = new BMap.Point(lng, lat);
						$(this).removeClass('hide').html('约' + (map.getDistance(pointA, pointB)/1000).toFixed(2)+' 公里');
					}
				});
			{/if}
			return true;
		}
		order_list_dist();
	});
});
</script>
{template 'manage/common'}
{template 'manage/footer'}